﻿using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using LB.Server.DAL.DomainEntitites;

namespace LB.Server.DAL.EntityFramework
{
    public class RepositoryDataContext : DbContext
    {
        /// <summary>
        /// Configuration of mapping entities to Db tables
        /// </summary>
        /// <param name="modelBuilder">Database model builder</param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            //--------------------------- Book ---------------------------------
            modelBuilder.Entity<Book>().HasKey(b => b.BookId);

            //--------------------------- Chapter ------------------------------
            modelBuilder.Entity<Chapter>().HasKey(c => c.ChapterId);

            //--------------------------- Part ---------------------------------
            modelBuilder.Entity<Part>().HasKey(p => p.PartId);

            //--------------------------- TextContent --------------------------
            modelBuilder.Entity<TextContent>().HasKey(t => new { t.ContentId, t.Version }); // Composite Primary key

            //--------------------------- User ---------------------------------
            modelBuilder.Entity<User>().HasKey(u => u.UserId);

            //--------------------------- WorkingSession -----------------------
            modelBuilder.Entity<WorkingSession>().HasKey(ws => ws.SessionId);
        }

        public DbSet<Book> Books { get; set; }

        public DbSet<User> Users { get; set; }

        public DbSet<Chapter> Chapters { get; set; }

        public DbSet<WorkingSession> WorkingSessions { get; set; }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                base.Dispose(true);
            }
            GC.SuppressFinalize(this);
        }

        public void Dispose()
        {
            Dispose(true);
        }

    }
}
